package org.gruposp2p.dnie.server.rest;

import com.sun.jersey.api.NotFoundException;
import com.sun.jersey.api.view.ImplicitProduces;
import com.sun.jersey.api.view.Viewable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gruposp2p.dnie.model.DNIeDocumentToSign;
import org.gruposp2p.dnie.model.DNIeUser;
import org.gruposp2p.dnie.service.DNIeDocumentToSignDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;

/**
 *
 * @author jj
 */
@Path(value = "/dnieDocumentToSign/{documentId}")
@Component
@ImplicitProduces("text/html;qs=5")
@Scope("request")
public class DnieDocumentToSignResource {

    private static Logger logger =
            LoggerFactory.getLogger(DnieDocumentToSignResource.class);
 
    @Autowired
    private DNIeDocumentToSignDAO documentToSignDao;
    @Context ServletContext servletContext;
    @Context HttpServletRequest request;

    @GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response get (@PathParam("documentId") Integer documentId) {
        DNIeDocumentToSign dnieDocumentToSign =
                documentToSignDao.findById(documentId);
        if (dnieDocumentToSign == null) {
            return Response.status(404).build();
        } else {
            return Response.ok(dnieDocumentToSign).build();
        }
    }

    @GET
    @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML})
    public Viewable getHML(@PathParam("documentId") Integer documentId) {
        DNIeDocumentToSign dnieDocumentToSign = documentToSignDao.findById(documentId);
        if (dnieDocumentToSign == null)
            throw new NotFoundException("DNIeDocumentToSign, " + documentId + ", is not found");
        dnieDocumentToSign.setUrl(request.getRequestURL().toString());
        request.setAttribute("document", dnieDocumentToSign);
        return new Viewable("documentToSign", null);
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public Response put (DNIeDocumentToSign dnieDocumentToSignReceived){
        DNIeDocumentToSign dnieDocumentToSign =
                documentToSignDao.update(dnieDocumentToSignReceived);
        return Response.ok().entity(dnieDocumentToSign).build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response post (DNIeDocumentToSign dnieDocumentToSignReceived){
        DNIeUser user = (DNIeUser) request.getAttribute("user");
        dnieDocumentToSignReceived.setDnieUserId(user);
        DNIeDocumentToSign dnieDocumentToSign =
                documentToSignDao.update(dnieDocumentToSignReceived);
        return Response.ok().entity(dnieDocumentToSign).build();
    }

    @DELETE
    public Response delete (@PathParam("documentId") Integer documentId) {
        documentToSignDao.delete(documentId);
        return Response.ok().build();
    }

}